package com.glsc.ngateway.opmanage.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GLSCDateUtil {

	public static String getStr_yyyyMMdd(Date date) {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd");
		if(date==null) {
			return null;
		}
		return sdf.format(date);
	}

	public static String getStr_yyyyMMdd_(Date date) {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		if(date==null) {
			return null;
		}
		return sdf.format(date);
	}

	public static String getStr_yyyyMMdd(LocalDate date) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
		if(date==null) {
			return null;
		}
		return date.format(df);
	}

	public static String getStr_yyyyMMdd_(LocalDate date) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		if(date==null) {
			return null;
		}
		return date.format(df);
	}

	public static String getStr_yyyyMMddHHMMSSSSS(LocalDateTime date) {

		DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
		if(date==null) {
			return null;
		}
		return date.format(df2);
	}

	public static String getStr_yyyyMMddHHMMSS(LocalDateTime date) {

		DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
		if(date==null) {
			return null;
		}
		return date.format(df3);
	}

	public static String getStr_yyyyMMddHHMMSS(long date) {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateformat.format(date);
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public static LocalDate getLocalDate(String date) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
		return LocalDate.parse(date.replaceAll("-", ""),df);
	}

	public static LocalDate getLocalDate(Date date) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
		return LocalDate.parse(getStr_yyyyMMdd(date),df);
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public static Date getUtilDate(java.sql.Date sqlDate) {
		return new Date (sqlDate.getTime());
	}

	public static Date getUtilDate(String date) {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd");
		try {
			return sdf.parse(date.replaceAll("-", ""));
		} catch (ParseException e) {
//			e.printStackTrace();
		}
		return null;
	}

	public static Date getUtilDate(LocalDate localDate) {
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
		return Date.from(instant);
	}

	public static Date getUtilDate(LocalDateTime localDatetime) {
		return Date.from(localDatetime.atZone( ZoneId.systemDefault()).toInstant());
	}



	/**
	 * 返回minutes分钟后的Date
	 * @param minutes
	 * @return
	 */
	public static Date futrueDate(int minutes) {
		LocalDateTime ldt = LocalDateTime.now();
		ldt = ldt.plusMinutes(minutes);
		return Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
	}



	public static String dtFormatter1Today() {
		return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
	}

	public static String now(){
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
		return now.format(formatter);
	}

	public static String nowCrossbar (){
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return now.format(formatter);
	}

	public static String nowyyyyMMdd(){
		LocalDateTime now = LocalDateTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		return now.format(formatter);
	}

	public static List<List<Date>> splitDatePair(Date startDate,Date endDate,int daysPerPair){
		List<List<LocalDate>> r = splitDatePair(getLocalDate(startDate),getLocalDate(endDate),daysPerPair);

		List<List<Date>> datePairList = new ArrayList<List<Date>>();
		r.forEach(l->{
			List<Date> dp = new ArrayList<Date>();
			dp.add(getUtilDate(l.get(0)));
			dp.add(getUtilDate(l.get(1)));
			datePairList.add(dp);
		});
		return datePairList;
	}

	public static List<List<LocalDate>> splitDatePair(LocalDate startDate,LocalDate endDate,int daysPerPair){
		List<List<LocalDate>> datePairList = new ArrayList<List<LocalDate>>();
		LocalDate tmpStartDate = LocalDate.of(startDate.getYear(), startDate.getMonthValue(), startDate.getDayOfMonth());
		LocalDate tmpEndDate = tmpStartDate.plusDays(daysPerPair-1);
		while(endDate.isAfter(tmpEndDate) || endDate.isEqual(tmpEndDate) ) {
			List<LocalDate> datePair = new ArrayList<LocalDate>();
			datePair.add(tmpStartDate);
			datePair.add(tmpEndDate);
			datePairList.add(datePair);
			tmpStartDate = tmpStartDate.plusDays(daysPerPair);
			tmpEndDate = tmpEndDate.plusDays(daysPerPair);
		}
		if(tmpStartDate.isBefore(endDate) || tmpStartDate.isEqual(endDate) ) {
			List<LocalDate> datePair = new ArrayList<LocalDate>();
			datePair.add(tmpStartDate);
			datePair.add(endDate);
			datePairList.add(datePair);
		}

		return datePairList;
	}


	public static  List<LocalDate> getAscDateList(LocalDate startDate,LocalDate endDate) {
	    if(startDate.isAfter(endDate)) {
	    	LocalDate td = startDate;
	    	startDate = endDate;
	    	endDate = td;
	    }
		List<LocalDate> result = new ArrayList<>();
	    LocalDate tempDate = null;//用户存储中间变量
	    Long num = endDate.toEpochDay() - startDate.toEpochDay();
	    for (int i = 0; i <= num; i++) {
	        tempDate = startDate;
	        if (tempDate.toEpochDay() - endDate.toEpochDay() <= 0) {
	            result.add(startDate);
	            startDate = tempDate.plusDays(1);
	        }
	    }
	    return result;
	}


	 public static void main(String[] args) {
//		 splitDatePair(LocalDate.of(2020, 1, 1),LocalDate.of(2020, 1, 20),5).forEach(r->{System.out.println(r);});

//		 splitDatePair(getUtilDate(LocalDate.of(2020, 1, 1)),getUtilDate(LocalDate.of(2020, 1, 20)),5).forEach(r->{System.out.println(r);});

		 getAscDateList(LocalDate.of(2020, 12, 4),LocalDate.of(2020, 12, 7)).forEach(r->{System.out.println(r);});
	 }
}
